package com.huawei.eidc.message;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.huawei.eidc.message.entity.Department;
import com.huawei.eidc.message.entity.Member;

public class Query {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
//		getDept("1000");
		
		queryMember("1000", "1025");

	}
	
	/**
	 * 根据企业编号查询所有部门
	 * @param ecID 企业编号
	 * @return	部门集合
	 * @throws Exception
	 */
	private static List<Department> getDept(String ecID) throws Exception{
		
		Document doc = DocumentHelper.createDocument();
		Element body = doc.addElement("BODY");
		Element page = body.addElement("PAGEINFO");
		page.addElement("CURRENTPAGE").setText("1");
		page.addElement("PAGESIZE").setText("10");
		body.addElement("ECID").setText(ecID);
//		body.addElement("DEPTNAME").setText("");	//可选
		String result = Soap.deptQuery("deptQuery",doc);
		Document document = DocumentHelper.parseText(result);
		
		if(!"0".equals(checkResult(document))) {
			return null;
		}
		
		Element deptList = document.getRootElement().element("DEPTLIST");
		
		Iterator ite = deptList.elementIterator();
		List<Department> list = new ArrayList<Department>();
		
		while(ite.hasNext()) {
			Element info = (Element)ite.next();
			Department dept = new Department();
			dept.setDeptID(info.elementText("DEPTID"));
			dept.setDeptName(info.elementText("DEPTNAME"));
			dept.setDeptParent(info.elementText("DEPTLEVEL"));
			dept.setDeptManager(info.elementText("DEPTMANGER"));
			list.add(dept);
		}
		return list;
	}
	
	/**
	 * 查询部门所有职员
	 * @param ecID	企业编号
	 * @param deptID	部门编号
	 * @return			职员集合
	 * @throws Exception
	 */
	private static List<Member> queryMember(String ecID, String deptID) throws Exception{
		
		Document doc = DocumentHelper.createDocument();
		Element body = doc.addElement("BODY");
		Element page = body.addElement("PAGEINFO");
		page.addElement("CURRENTPAGE").setText("1");
		page.addElement("PAGESIZE").setText("10");
		body.addElement("ECID").setText(ecID);
		body.addElement("USERID").setText("10043443");	//可选 华为接口问题，随便填个值即可
		body.addElement("DEPTID").setText(deptID);	//可选
		
		String result = Soap.addressQuery("addressQuery",doc);
		Document document = DocumentHelper.parseText(result);
		
		if(!"0".equals(checkResult(document))) {
			return null;
		}
		
		Element memberList = document.getRootElement().element("MEMBERLIST");
		
		Iterator ite = memberList.elementIterator();
		List<Member> list = new ArrayList<Member>();
		
		while(ite.hasNext()) {
			Element info = (Element)ite.next();
			Member member = new Member();
			member.setMemberID(info.elementText("MEMBERID"));
			member.setMemberName(info.elementText("MEMBERNAME"));
			member.setMobile(info.elementText("MOBILE"));
			member.setMemberType(info.elementText("MEMBERTYPE"));
			member.setDeptID(deptID);
			member.setDeptName(info.elementText("DEPTNAME"));
			
			list.add(member);
		}
		
		return list;
	}
	
	private static String checkResult(Document document) {
		Element body = document.getRootElement();
		String resultCode = body.elementText("RESULTCODE");
		Element total = body.element("RECORDTOTAL");
		if ("0".equals(resultCode) && total != null) {
			return "成功";
		}else {
			System.out.println(resultCode+" :" + body.elementText("RESULTMSG"));
			return resultCode;
		}
	}

}
